package com.irononetech.android.homecomponent;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;

import com.irononetech.android.Application.Application;
import com.irononetech.android.fileio.FileOperations;
import com.irononetech.android.formcomponent.view.DraftActivity;
import com.irononetech.android.searchcomponent.SearchUIobject;
import com.irononetech.android.template.EventParcel;
import com.irononetech.android.template.UIEvent;
import com.irononetech.android.claimsonersa.R;

public class HomeActivity extends Activity {
	Logger LOG = LoggerFactory.getLogger(HomeActivity.class);
	public ProgressDialog dialog;

	public synchronized ProgressDialog getDialog() {
		return dialog;
	}

	public synchronized void setDialog(ProgressDialog pd) {
		dialog = pd;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		LOG.debug("ENTRY onCreate");
		try {
			super.onCreate(savedInstanceState);

			// overridePendingTransition(R.anim.slide_left, R.anim.slide_left);
			overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
			setContentView(R.layout.home);
			LOG.debug("SUCCESS onCreate");
		} catch (Exception e) {
			LOG.error("onCreate:10151");
			if (e != null) {
				LOG.error("Message: " + e.getMessage());
				LOG.error("StackTrace: " + e.getStackTrace());
			}
		}
	}

	@Override
	protected void onPause() {
		try {
			LOG.debug("ENTRY onPause");
			// overridePendingTransition(R.anim.slide_left, R.anim.slide_left);
			overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
			super.onPause();
			LOG.debug("SUCCESS onPause");
		} catch (Exception e) {
			LOG.error("onCreate:10152");
			if (e != null) {
				LOG.error("Message: " + e.getMessage());
				LOG.error("StackTrace: " + e.getStackTrace());
			}
		}
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}

	public void on_motor_claim_form_button_click(View v) {
		try {
			LOG.debug("ENTRY on_motor_claim_form_button_click");
			Application.setIsInVisitSearchPage(false);
			if (FileOperations.draftsMaxReached()) { // 10
				AlertDialog.Builder alertbox = new AlertDialog.Builder(
						HomeActivity.this);
				alertbox.setTitle("Draft Alert:");
				alertbox.setMessage(getString(R.string.max_no_of_drafts_10_has_been_reached_please_deal_with_some_drafts_to_continue_));
				alertbox.setPositiveButton("Ok",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface arg0, int arg1) {
								Intent intent = new Intent(HomeActivity.this,
										DraftActivity.class);
								intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
								HomeActivity.this.startActivity(intent);
								// finish();
							}
						});
				alertbox.show();
			} else if (FileOperations.draftsMaxReachedWarning()) { // 9
				AlertDialog.Builder alertbox = new AlertDialog.Builder(
						HomeActivity.this);
				alertbox.setTitle(R.string.draft_exceed_warning_);
				alertbox.setMessage(getString(R.string.draft_warn_msg2));
				alertbox.setPositiveButton("Ok",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface arg0, int arg1) {
								Application.getInstance().doActionOnEvent(
										new EventParcel(
												UIEvent.B_X_BUTTON_CLICK,
												HomeActivity.this, null));
							}
						});
				alertbox.show();
			} else if (FileOperations.draftsWarningMsg()) { // 8
				AlertDialog.Builder alertbox = new AlertDialog.Builder(
						HomeActivity.this);
				alertbox.setTitle(R.string.draft_exceed_warning_);
				alertbox.setMessage(getString(R.string.draft_warn_msg));
				alertbox.setPositiveButton("Ok",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface arg0, int arg1) {
								Application.getInstance().doActionOnEvent(
										new EventParcel(
												UIEvent.B_X_BUTTON_CLICK,
												HomeActivity.this, null));
							}
						});
				alertbox.show();
			} else {
				Application.getInstance().doActionOnEvent(
						new EventParcel(UIEvent.B_X_BUTTON_CLICK, this, null));
			}
			LOG.debug("SUCCESS on_motor_claim_form_button_click");
		} catch (Exception e) {
			LOG.error("on_motor_claim_form_button_click:10153");
			if (e != null) {
				LOG.error("Message: " + e.getMessage());
				LOG.error("StackTrace: " + e.getStackTrace());
			}
		}
	}

	public void on_click_visit_button(View V) {
		try {
			LOG.debug("ENTRY on_click_visit_button");
			if (FileOperations.draftsMaxReachedForVisits()) { // 10
				AlertDialog.Builder alertbox = new AlertDialog.Builder(
						HomeActivity.this);
				alertbox.setTitle("Draft Alert:");
				alertbox.setMessage(getString(R.string.max_no_of_drafts_10_has_been_reached_please_deal_with_some_drafts_to_continue_));
				alertbox.setPositiveButton("Ok",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface arg0, int arg1) {
								Intent intent = new Intent(HomeActivity.this,
										DraftActivity.class);
								intent.putExtra("TAB", "1");
								intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
								HomeActivity.this.startActivity(intent);
								// finish();
							}
						});
				alertbox.show();
			} else if (FileOperations.draftsMaxReachedWarningForVisits()) { // 9
				AlertDialog.Builder alertbox = new AlertDialog.Builder(
						HomeActivity.this);
				alertbox.setTitle(R.string.draft_exceed_warning_);
				alertbox.setMessage(getString(R.string.draft_warn_msg2));
				alertbox.setPositiveButton("Ok",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface arg0, int arg1) {
								// Application.getInstance().doActionOnEvent(new
								// EventParcel(UIEvent.B_X_BUTTON_CLICK,
								// HomeActivity.this, null));
								commonVisitViewCode();
							}
						});
				alertbox.show();
			} else if (FileOperations.draftsWarningMsgForVisits()) { // 8
				AlertDialog.Builder alertbox = new AlertDialog.Builder(
						HomeActivity.this);
				alertbox.setTitle(R.string.draft_exceed_warning_);
				alertbox.setMessage(getString(R.string.draft_warn_msg));
				alertbox.setPositiveButton("Ok",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface arg0, int arg1) {
								// Application.getInstance().doActionOnEvent(new
								// EventParcel(UIEvent.B_X_BUTTON_CLICK,
								// HomeActivity.this, null));
								commonVisitViewCode();
							}
						});
				alertbox.show();
			} else {
				commonVisitViewCode();
				// Application.getInstance().doActionOnEvent(new
				// EventParcel(UIEvent.B_X_BUTTON_CLICK, this, null));
			}

			LOG.debug("SUCCESS  on_click_visit_button");

		} catch (Exception e) {
			 LOG.error("on_click_visit_button:11156");
			   if(e != null){
				LOG.error("Message: " + e.getMessage());
				LOG.error("StackTrace: " + e.getStackTrace());
			  }
		}

	}

	private void commonVisitViewCode() {
		SearchUIobject searchUIobject = new SearchUIobject();
		searchUIobject.setJobOrVehicleNo("Sample-VehNo");
		Application.setIsInVisitSearchPage(true);
		Application.getInstance().doActionOnEvent(
				new EventParcel(UIEvent.HOME_SEARCH_BUTTON_CLICK, this, null));
	}

	public void on_click_drafts_button(View V) {
		try {
			LOG.debug("ENTRY on_click_drafts_button");
			Application.setIsInVisitSearchPage(false);
			Application.getInstance().doActionOnEvent(
					new EventParcel(UIEvent.HOME_DRAFTS_BUTTON_CLICK, this,
							null));

			LOG.debug("SUCCESS on_click_drafts_button");
		} catch (Exception e) {
			 LOG.error("on_click_drafts_button:11155");
			   if(e != null){
				LOG.error("Message: " + e.getMessage());
				LOG.error("StackTrace: " + e.getStackTrace());
			  }
		}
	}

	public void on_click_search_button(View v) {
		try {
			LOG.debug("ENTRY on_click_search_button");
			SearchUIobject searchUIobject = new SearchUIobject();
			searchUIobject.setJobOrVehicleNo("Sample-VehNo");
			Application.setIsInVisitSearchPage(false);
			Application.setIsVisit(false);
			Application.getInstance().doActionOnEvent(
					new EventParcel(UIEvent.HOME_SEARCH_BUTTON_CLICK, this,
							null));
			LOG.debug("SUCCESS on_click_search_button");
		} catch (Exception e) {
			 LOG.error("on_click_search_button:10156");
			   if(e != null){
				LOG.error("Message: " + e.getMessage());
				LOG.error("StackTrace: " + e.getStackTrace());
			  }
		}
	}

	public void on_click_copy_images_button(View v) {
		try {
			LOG.debug("ENTRY on_click_copy_images_button");
			//Application.setIsInVisitSearchPage(false);
			//Application.copyImageCommon(this, v);
			
			AlertDialog.Builder alertbox = new AlertDialog.Builder(
					HomeActivity.this);
			alertbox.setTitle("Copy Images:");
			alertbox.setMessage("Use the Camera app to capture the images.");
			alertbox.setPositiveButton("Ok",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface arg0, int arg1) {							
						}
					});
			alertbox.show();
			
			LOG.debug("SUCCESS on_click_copy_images_button");
		} catch (Exception e) {
			 LOG.error("on_click_copy_images_button:10155");
			   if(e != null){
				LOG.error("Message: " + e.getMessage());
				LOG.error("StackTrace: " + e.getStackTrace());
			  }
		}
	}

	public void on_click_logout_button(View v) {
		try {
			final EventParcel ep = new EventParcel(UIEvent.LOGOUT_BUTTON_CLICK,
					this, null);

			AlertDialog.Builder alertbox = new AlertDialog.Builder(
					HomeActivity.this);
			alertbox.setTitle(R.string.logout_confirm_title);
			alertbox.setMessage(getString(R.string.logout_confirm));
			alertbox.setPositiveButton("Yes",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface arg0, int arg1) {
							LOG.debug("ENTRY on_click_logout_button");
							Application.getInstance().doActionOnEvent(ep);
							Application.stopImgUploder();
							LOG.debug("SUCCESS on_click_logout_button");
						}
					});
			alertbox.setNegativeButton("No",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface arg0, int arg1) {
						}
					});
			alertbox.show();
		} catch (Exception e) {
			 LOG.error("on_click_logout_button:10157");
			   if(e != null){
				LOG.error("Message: " + e.getMessage());
				LOG.error("StackTrace: " + e.getStackTrace());
			  }
		}
	}

	// can remove
	public void on_click_pending_jobs(View V) {
		try {
			LOG.debug("ENTRY on_click_pending_jobs");
			Application.getInstance().doActionOnEvent(
					new EventParcel(UIEvent.HOME_PENDINGJOBS_BUTTON_CLICK,
							this, null));
			LOG.debug("SUCCESS on_click_pending_jobs");
		} catch (Exception e) {
			 LOG.error("on_click_pending_jobs:10155");
			   if(e != null){
				LOG.error("Message: " + e.getMessage());
				LOG.error("StackTrace: " + e.getStackTrace());
			  }
		}
	}

}